package com.woniu.service.medicalService.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.model.gh.common.es.HospitalRepository;
import com.woniu.model.gh.utils.DictionaryUtil;
import com.woniu.model.gh.utils.MapUtil;
import com.woniu.model.gh.utils.PageVo;
import com.woniu.model.gh.utils.TypeVo;
import com.woniu.mapper.medicalService.HospitalMapper;
import com.woniu.service.medicalService.IHospitalService;
import com.woniu.model.medicalService.entity.Hospital;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//医院表
@Service
public class HospitalServiceImpl extends ServiceImpl<HospitalMapper, Hospital> implements IHospitalService {

    @Autowired
    private ElasticsearchRestTemplate esRestTemplate;

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private RedisTemplate redisTemplate;

    //按医院名称查询 分词
    @Override
    public PageVo<Hospital> findByName(Map<String, Object> condition) {

        //获取参数
        Integer pageNum = MapUtil.getInteger("pageNum", 1, condition);
        Integer pageSize = MapUtil.getInteger("pageSize", 5, condition);
        String name = MapUtil.getString("name", condition);

        //条件查询数据库
        List<Hospital> hospitalList = baseMapper.findByName(condition);

        if (name != null && !name.equals("")) {
            //将查询出来的数据存储到es
            hospitalRepository.saveAll(hospitalList);

            //实现高亮 分词查询
            List<Hospital> list = getHighLight(name);

            return new PageVo<>(pageNum, pageSize, list);
        }

        return new PageVo<>(pageNum, pageSize, hospitalList);
    }

    //实现高亮 分词查询
    private List<Hospital> getHighLight(String keyword) {

        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.multiMatchQuery(keyword, "name"));
        HighlightBuilder.Field[] fields = new HighlightBuilder.Field[1];

        fields[0] = new HighlightBuilder
                .Field("name")
                .preTags("<span style = 'color:red'>")
                .postTags("</span >");
        builder.withHighlightFields(fields);

        SearchHits<Hospital> searchHits = esRestTemplate.search(builder.build(), Hospital.class);

        return searchHits.stream().map(searchHit -> {
            Hospital hospital = searchHit.getContent();
            List<String> highlightList = searchHit.getHighlightField("name");

            if (CollectionUtils.isEmpty(highlightList)) return hospital;

            StringBuilder name = new StringBuilder();
            for (String str : highlightList) name.append(str);
            hospital.setName(name.toString());

            return hospital;
        }).collect(Collectors.toList());

    }

    //获取医院类型
    @Override
    public List<TypeVo> getType() {
        return DictionaryUtil.getDictionaryList(redisTemplate, 5);
    }

    //获取医院等级
    @Override
    public List<TypeVo> getGrade() {
        return DictionaryUtil.getDictionaryList(redisTemplate, 4);
    }

    //获取添加后的最后一页
    @Override
    public int getLastPage() {
        int count = baseMapper.getCount();
        if (count%5==0) return count/5;
        else return count/5+1;
    }

}
